﻿@inject IPopupService PopupService
@inject IDataCacheService _dataCacheService

@if (isReady)
{
    <MDCard Outline="Outline" Class="h-100">
        @if (Model.UserScores.Any(s => s.Socres.IsScored))
        {
            <div style="display: flex; justify-content: flex-end;">
                <div style="max-width:100px;">
                    <CnGalWebSite.Components.Inputs.MasaSelecte Value="scope" ValueChanged="ScopeValueChanged" TValue="PlayedGameCountScope" Class="pe-4 pt-0" />
                </div>
                <div style="max-width:100px;">
                    <CnGalWebSite.Components.Inputs.MasaSelecte Value="type" ValueChanged="TypeValueChanged" TValue="PlayedGameScoreType" Class="pe-4 pt-0" />
                </div>
            </div>
            <div class="d-flex align-center justify-center">
                @if (isReady)
                {
                    <MECharts Option="model" Width="@("100%")" Height="300" @ref="echarts" Dark="_dataCacheService.ThemeSetting.IsDark">
                    </MECharts>
                }

            </div>

        }
        else
        {
            <div>
                暂无评分
            </div>
        }
    </MDCard>
}



@code {
    [Parameter]
    public bool Outline { get; set; }

    [Parameter]
    public PlayedGameOverviewModel Model { get; set; }

    EChartsOptionModel model = new EChartsOptionModel();

    MECharts echarts;

    bool isReady;
    PlayedGameScoreType type;
    PlayedGameCountScope scope;

    protected override void OnParametersSet()
    {
        Refresh();
    }

    public void Refresh()
    {
        if (model.XAxis.Data.Any() == false)
        {
            model.XAxis.Data.AddRange(new List<string> { "1分", "2分", "3分", "4分", "5分", "6分", "7分", "8分", "9分", "10分" });
        }

        if (model.Legend.Data.Any() == false)
        {
            model.Legend.Data.Add("人数");
        }

        if (model.Series.Any() == false)
        {
            model.Series.Add(new EChartsOptionSery
            {
                Name = "人数",
                Stack = "default",
                Type = "bar"
            });

        }

        var series = model.Series.First().Data;
        series.Clear();

        if (Model.UserScores.Any(s => s.Socres.IsScored) == false)
        {
            isReady = true;
            return;
        }

        if (Model.UserScores.Any(s => scope == PlayedGameCountScope.All || (string.IsNullOrWhiteSpace(s.PlayImpressions) == false && s.PlayImpressions.Length > ToolHelper.MinValidPlayImpressionsLength)) == false)
        {
            isReady = true;
            return;
        }

        var data = Model.UserScores.Where(s => scope == PlayedGameCountScope.All || (string.IsNullOrWhiteSpace(s.PlayImpressions) == false && s.PlayImpressions.Length > ToolHelper.MinValidPlayImpressionsLength)).Select(s => type switch
        {
            PlayedGameScoreType.Script => s.Socres.ScriptSocre,
            PlayedGameScoreType.System => s.Socres.SystemSocre,
            PlayedGameScoreType.CV => s.Socres.CVSocre,
            PlayedGameScoreType.Total => s.Socres.TotalSocre,
            PlayedGameScoreType.Music => s.Socres.MusicSocre,
            PlayedGameScoreType.Paint => s.Socres.PaintSocre,
            PlayedGameScoreType.Show => s.Socres.ShowSocre,
            _ => 0
        });



        series.AddRange(new List<double>
{
            data.Count(s=>s==1),
            data.Count(s=>s==2),
            data.Count(s=>s==3),
            data.Count(s=>s==4),
            data.Count(s=>s==5),
            data.Count(s=>s==6),
            data.Count(s=>s==7),
            data.Count(s=>s==8),
            data.Count(s=>s==9),
            data.Count(s=>s==10),
        });



        //if (echarts != null)
        //{
        //    await echarts.DisposeECharts();
        //}
        isReady = true;
        StateHasChanged();
    }

    public async Task TypeValueChanged(PlayedGameScoreType _type)
    {
        if (Model.IsDubbing == false && _type == PlayedGameScoreType.CV)
        {
            await PopupService.ToastAsync(new ToastConfig
            {
                Type = AlertTypes.Error,
                Title = "这个游戏没有配音哦~",
                    Duration = 4000
            });
            return;
        }
        type = _type;
        Refresh();

    }
    public void ScopeValueChanged(PlayedGameCountScope _scope)
    {
        scope = _scope;
        Refresh();
    }
}
